# -*- coding: utf-8 -*-
# !/usr/bin/env python
from rest_framework.views import APIView

from components.resource_management.env_hosts.dao import query_env_hosts_soft
from utils.views import Result

class EnvHostsSoft(APIView):
    def get(self, request):
        data = request.query_params
        host_data_list = []
        if data['env_id']:
            host_data_list = query_env_hosts_soft(data['env_id'])

        data_swap_list = []
        return_list = []
        for host_data_list_one in host_data_list:
            soft_data = {
                "package_id": host_data_list_one.pop("package_id"),
                "soft_install_code_text": host_data_list_one.pop("soft_install_code_text"),
                "soft_code_text": host_data_list_one.pop("soft_code_text"),
                "soft_id": host_data_list_one.pop("soft_id"),
                "soft_nick": host_data_list_one.pop("soft_nick"),
                "soft_code": host_data_list_one.pop("soft_code"),
                "soft_install_on": host_data_list_one.pop("soft_install_on"),
                "soft_log_path": host_data_list_one.pop("soft_log_path"),
                "soft_install_code": host_data_list_one.pop("soft_install_code"),
                "soft_install_url": host_data_list_one.pop("soft_install_url"),
                "soft_install_cmd": host_data_list_one.pop("soft_install_cmd"),
                "soft_start_cmd": host_data_list_one.pop("soft_start_cmd"),
                "soft_restart_cmd": host_data_list_one.pop("soft_restart_cmd"),
                "soft_stop_cmd": host_data_list_one.pop("soft_stop_cmd"),
                "comment": host_data_list_one.pop("soft_comment")
            }

            if host_data_list_one['host_id'] in data_swap_list:
                for return_list_one in return_list:
                    if return_list_one['host_id'] == host_data_list_one['host_id']:
                        return_list_one['software_data'].append(soft_data)
            else:
                data_swap_list.append(host_data_list_one['host_id'])
                host_data_list_one['software_data'] = [soft_data]
                return_list.append(host_data_list_one)



        return Result.success_response(data=return_list, msg='查询成功')